package com.sks.web.action.privilege;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.stereotype.Controller;

import com.sks.bean.privilege.PrivilegeGroup;
import com.sks.bean.privilege.SystemPrivilege;
import com.sks.bean.privilege.SystemPrivilegePK;
import com.sks.service.privilege.PrivilegeGroupService;
import com.sks.service.privilege.SystemPrivilegeService;
import com.sks.utils.SiteUrl;
import com.sks.web.action.BasicAction;
import com.sks.web.formbean.privilege.PrivilegeGroupForm;

@Controller("/cms/privilege/privilegegroup/manage")
public class PrivilegeGroupManageAction extends BasicAction {
	@Resource SystemPrivilegeService privilegeService;
	@Resource PrivilegeGroupService groupService;
	/**
	 * 权限组添加界面
	 */
	@Permission(module="employee", privilege="privilegeGroupSet")
	public ActionForward addUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		this.saveToken(request);
		PrivilegeGroupForm formbean = (PrivilegeGroupForm)form;
		formbean.setMethod(ADD);
		setBaseData(request);
		return mapping.findForward(EDIT);
	}
	/**
	 * 添加权限组
	 */
	@Permission(module="employee", privilege="privilegeGroupSet")
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		PrivilegeGroupForm formbean = (PrivilegeGroupForm)form;
		PrivilegeGroup group = new PrivilegeGroup();
		group.setName(formbean.getName());
		if(formbean.getPrivileges()!=null && formbean.getPrivileges().length>0){
			for(SystemPrivilegePK id : formbean.getPrivileges()){
				group.getPrivileges().add(new SystemPrivilege(id));
			}
		}
		groupService.save(group);
		request.setAttribute("message", "权限组添加成功");
		request.setAttribute("urladdress", SiteUrl.readUrl("control.privilegegroup.list"));
		return mapping.findForward("message");
	}
	
	/**
	 * 权限组修改界面
	 */
	@Permission(module="employee", privilege="PrivilegeGroupList")
	public ActionForward editUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		this.saveToken(request);
		PrivilegeGroupForm formbean = (PrivilegeGroupForm)form;
		PrivilegeGroup group = groupService.find(formbean.getGroupid());
		formbean.setName(group.getName());
		formbean.setMethod(EDIT);
		request.setAttribute("selectprivileges", group.getPrivileges());
		setBaseData(request);
		return mapping.findForward(EDIT);
	}
	
	/**
	 * 修改权限组
	 */
	@Permission(module="employee", privilege="privilegeGroupSet")
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		PrivilegeGroupForm formbean = (PrivilegeGroupForm)form;
		PrivilegeGroup group = groupService.find(formbean.getGroupid());
		group.setName(formbean.getName());
		group.getPrivileges().clear();
		if(formbean.getPrivileges()!=null && formbean.getPrivileges().length>0){
			for(SystemPrivilegePK id : formbean.getPrivileges()){
				group.getPrivileges().add(new SystemPrivilege(id));
			}
		}
		groupService.update(group);
		request.setAttribute("message", "权限组修改成功");
		request.setAttribute("urladdress", SiteUrl.readUrl("control.privilegegroup.list"));
		return mapping.findForward("message");
	}
	
	/**
	 * 删除权限组
	 */
	@Permission(module="employee", privilege="privilegeGroupSet")
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		PrivilegeGroupForm formbean = (PrivilegeGroupForm)form;
		groupService.delete(formbean.getGroupid());
		request.setAttribute("message", "权限组删除成功");
		request.setAttribute("urladdress", SiteUrl.readUrl("control.privilegegroup.list"));
		return mapping.findForward("message");
	}
	
	private void setBaseData(HttpServletRequest request) {
		//request.setAttribute("privileges", privilegeService.getScrollData().getResultlist());
		List<SystemPrivilege> hb = privilegeService.getPrivilegeByModule("house", "building", "info", "message");
		List<SystemPrivilege> internal = privilegeService.getPrivilegeByModule("department", "employee", "users");
		List<SystemPrivilege> fn = privilegeService.getPrivilegeByModule("feature", "news");
		request.setAttribute("hb", hb);
		request.setAttribute("internal", internal);
		request.setAttribute("fn", fn);
	}
}
